package com.lqq.eduservice.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lqq.commonutils.R;
import com.lqq.eduservice.entity.EduCourse;
import com.lqq.eduservice.entity.vo.CourseInfoVo;
import com.lqq.eduservice.entity.vo.CoursePublishVo;
import com.lqq.eduservice.entity.vo.CourseQuery;
import com.lqq.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-29
 */
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin//解决跨越
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    //添加课程信息
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        //返回添加之后的课程id，为了后面添加大纲做准备
        String id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId", id);
    }

    //根据课程id查询课程信息
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    //修改课程信息
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //最终发布页面数据
    @GetMapping("/getCoursePublishInfo/{id}")
    public R getCoursePublishInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse", coursePublishVo);
    }

    //最终发布
    //修改课程状态edu_course字段status，0为未发布，1为已发布
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = courseService.getById(id);
        eduCourse.setStatus(1);
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //课程列表，条件查询带分页
    @PostMapping("/list/{current}/{limit}")
    public R list(@PathVariable("current") Long current,
                  @PathVariable("limit") Long limit,
                  @RequestBody(required = false) CourseQuery courseQuery) {

        //分页
        Page<EduCourse> page = new Page<>(current, limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        //获取查询条件
        String title = courseQuery.getTitle();
        Integer status = courseQuery.getStatus();
        String oneSubject = courseQuery.getSubjectParentId();
        String twoSubject = courseQuery.getSubjectId();

        if (!StringUtils.isEmpty(title)) {//加入条件1
            wrapper.like("title", title);//模糊查询
        }

        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        if (!StringUtils.isEmpty(oneSubject)) {
            wrapper.eq("subject_parent_id", oneSubject);
        }
        if (!StringUtils.isEmpty(twoSubject)) {
            wrapper.eq("subject_id", twoSubject);
        }

        wrapper.orderByDesc("gmt_create");

        IPage<EduCourse> coursePage = courseService.page(page, wrapper);
        long total = coursePage.getTotal();
        List<EduCourse> records = coursePage.getRecords();

        Map map = new HashMap();
        map.put("total", total);
        map.put("items", records);

        return R.ok().data("list", map);
    }
    //课程列表
    //获取所有
    @GetMapping("/list")
    public R getList() {
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list", list);
    }

    //根据id删除课程，级联删除，需要先删除 小节表——》章节表 ——》课程描述表 ——》课程表
    @DeleteMapping("/delete/{courseId}")
    public R removeById(@PathVariable String courseId) {
        courseService.removeCourse(courseId);
        return R.ok();
    }

}

